home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / PowerD_mui / examples / Class3.d < prev    next >
Encoding:
Text File  |  2002-03-13  |  8.3 KB  |  273 lines

  1. /*
  2. ** Demosource on how to use customclasses in D.
  3. ** Based on the C example 'Class3.c' by Stafan Stuntz.
  4. ** Translated to E by Sven Steiniger
  5. ** Translated to D by Martin <MarK> Kuchinka
  6. */
  7.  
  8. OPT    OPTIMIZE
  9.  
  10. MODULE    'muimaster',
  11.             'libraries/mui',
  12.             'intuition/classes',
  13.             'intuition/classusr',
  14.             'intuition/screens',
  15.             'intuition/intuition',
  16.             'utility/tagitem',
  17.             'lib/amiga'
  18.  
  19. /***************************************************************************/
  20. /* Here is the beginning of our simple new class...                        */
  21. /***************************************************************************/
  22.  
  23. /*
  24. ** This is the instance data for our custom class.
  25. */
  26.  
  27. OBJECT mydata
  28.     x,y,sx,sy
  29.  
  30. /*
  31. ** AskMinMax method will be called before the window is opened
  32. ** and before layout takes place. We need to tell MUI the
  33. ** minimum, maximum and default size of our object.
  34. */
  35.  
  36. PROC mAskMinMax(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_AskMinMax)(L)
  37.  
  38. /*
  39. ** let our superclass first fill in what it thinks about sizes.
  40. ** this will e.g. add the size of frame and inner spacing.
  41. */
  42.  
  43.     DoSuperMethodA(cl,obj,msg)
  44.  
  45. /*
  46. ** now add the values specific to our object. note that we
  47. ** indeed need TO *add* these values, not just set them!
  48. */
  49.  
  50.     msg.MinMaxInfo.MinWidth+=100
  51.     msg.MinMaxInfo.DefWidth+=120
  52.     msg.MinMaxInfo.MaxWidth+=500
  53.  
  54.     msg.MinMaxInfo.MinHeight+=40
  55.     msg.MinMaxInfo.DefHeight+=90
  56.     msg.MinMaxInfo.MaxHeight+=300
  57.  
  58. ENDPROC 0
  59.  
  60.  
  61. /*
  62. ** Draw method is called whenever MUI feels we should render
  63. ** our object. This usually happens after layout is finished
  64. ** or when we need to refresh in a simplerefresh window.
  65. ** Note: You may only render within the rectangle
  66. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  67. */
  68.  
  69. PROC mDraw(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_Draw)(L)
  70.     DEF    data:PTR TO mydata
  71.  
  72.     data:=INST_DATA(cl,obj)
  73.  
  74. /*
  75. ** let our superclass draw itself first, area class would
  76. ** e.g. draw the frame and clear the whole region. What
  77. ** it does exactly depends on msg.flags.
  78. **
  79. ** Note: You *must* call the super method prior to do
  80. ** anything else, otherwise msg.flags will not be set
  81. ** properly !!!
  82. */
  83.  
  84.     DoSuperMethodA(cl,obj,msg)
  85.  
  86. /*
  87. ** IF MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  88. ** MUI just wanted to update the frame or something like that.
  89. */
  90.  
  91.     IF msg.flags & MADF_DRAWUPDATE  /* called from our input method */
  92.         IF data.sx|data.sy
  93.             SetBPen(_rp(obj),_dri(obj).Pens[SHINEPEN])
  94.             ScrollRaster(_rp(obj),data.sx,data.sy,_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj))
  95.             SetBPen(_rp(obj),0)
  96.             data.sx:=0
  97.             data.sy:=0
  98.         ELSE
  99.             SetAPen(_rp(obj),_dri(obj).Pens[SHADOWPEN])
  100.             WritePixel(_rp(obj),data.x,data.y)
  101.         ENDIF
  102.     ELSEIF msg.flags & MADF_DRAWOBJECT
  103.         SetAPen(_rp(obj),_dri(obj).Pens[SHINEPEN])
  104.         RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj))
  105.     ENDIF
  106. ENDPROC 0
  107.  
  108.  
  109. PROC mSetup(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_HandleInput)(L)
  110.  
  111.     IFN DoSuperMethodA(cl,obj,msg) THEN RETURN FALSE
  112.     MUI_RequestIDCMP(obj,IDCMP_MOUSEBUTTONS | IDCMP_RAWKEY)
  113.  
  114. ENDPROC MUI_TRUE
  115.  
  116.  
  117. PROC mCleanup(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_HandleInput)(L)
  118.  
  119.     MUI_RejectIDCMP(obj,IDCMP_MOUSEBUTTONS | IDCMP_RAWKEY)
  120.  
  121. ENDPROC DoSuperMethodA(cl,obj,msg)
  122.  
  123.  
  124. /* in mSetup() we said that we want get a message IF mousebuttons or keys pressed
  125. ** so we have to define the input-handler
  126. ** Note : this is really a good example, because it shows how to use critical events
  127. **        carefully:
  128. **        IDCMP_MOUSEMOVE is only needed when left-mousebutton is pressed, so
  129. **        we dont request this until we get a SELECTDOWN-message and we reject
  130. **        IDCMP_MOUSEMOVE immeditly after we get a SELECTUP-message
  131. */
  132.  
  133. PROC mHandleInput(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_HandleInput)(L)
  134. #define _between(a,x,b) (((x)>=(a)) AND ((x)<=(b)))
  135. #define _isinobject(x,y) (_between(_mleft(obj),(x),_mright(obj)) AND _between(_mtop(obj),(y),_bottom(obj)))
  136.  
  137.     DEF    data:PTR TO mydata
  138.  
  139.     data:=INST_DATA(cl,obj)
  140.  
  141.     SELECT msg.muikey
  142.     CASE MUIKEY_LEFT   ; data.sx:=-1; MUI_Redraw(obj,MADF_DRAWUPDATE)
  143.     CASE MUIKEY_RIGHT  ; data.sx:= 1; MUI_Redraw(obj,MADF_DRAWUPDATE)
  144.     CASE MUIKEY_UP     ; data.sy:=-1; MUI_Redraw(obj,MADF_DRAWUPDATE)
  145.     CASE MUIKEY_DOWN   ; data.sy:= 1; MUI_Redraw(obj,MADF_DRAWUPDATE)
  146.     ENDSELECT
  147.  
  148.     IF msg.imsg
  149.         SELECT msg.imsg.Class
  150.         CASE IDCMP_MOUSEBUTTONS
  151.             IF msg.imsg.Code=SELECTDOWN
  152.                 IF _isinobject(msg.imsg.MouseX,msg.imsg.MouseY)
  153.                     data.x:=msg.imsg.MouseX
  154.                     data.y:=msg.imsg.MouseY
  155.                     MUI_Redraw(obj,MADF_DRAWUPDATE)
  156.  
  157.                     -> only request IDCMP_MOUSEMOVE if we realy need it
  158.                     MUI_RequestIDCMP(obj,IDCMP_MOUSEMOVE)
  159.                 ENDIF
  160.             ELSE
  161.                 -> reject IDCMP_MOUSEMOVE because THEN lmb is no longer pressed
  162.                 MUI_RejectIDCMP(obj,IDCMP_MOUSEMOVE)
  163.             ENDIF
  164.         CASE IDCMP_MOUSEMOVE
  165.             IF _isinobject(msg.imsg.MouseX,msg.imsg.MouseY)
  166.                 data.x:=msg.imsg.MouseX
  167.                 data.y:=msg.imsg.MouseY
  168.                 MUI_Redraw(obj,MADF_DRAWUPDATE)
  169.             ENDIF
  170.         ENDSELECT
  171.     ENDIF
  172.  
  173. ENDPROC DoSuperMethodA(cl,obj,msg)
  174.  
  175. /*
  176. ** Here comes the dispatcher for our custom class.
  177. ** Unknown/unused methods are passed to the superclass immediately.
  178. */
  179.  
  180. PROC MyDispatcher(cl:PTR TO IClass IN a0,obj IN a2,msg:PTR TO Msg IN a1)(LONG)
  181.  
  182.     SELECT msg.MethodID
  183.     CASE MUIM_AskMinMax    ;  RETURN mAskMinMax  (cl,obj,msg)
  184.     CASE MUIM_Draw         ;  RETURN mDraw       (cl,obj,msg)
  185.     CASE MUIM_HandleInput  ;  RETURN mHandleInput(cl,obj,msg)
  186.     CASE MUIM_Setup        ;  RETURN mSetup      (cl,obj,msg)
  187.     CASE MUIM_Cleanup      ;  RETURN mCleanup    (cl,obj,msg)
  188.     ENDSELECT
  189.  
  190. ENDPROC DoSuperMethodA(cl,obj,msg)
  191.  
  192. DEF    MUIMasterBase
  193.  
  194. /***************************************************************************/
  195. /* Thats all there is about it. Now lets see how things are used...        */
  196. /***************************************************************************/
  197.  
  198. PROC main()
  199.     DEF    app=NIL,window,myobj,
  200.             mcc=NIL:PTR TO MUI_CustomClass,
  201.             sigs=0
  202.  
  203.     IFN MUIMasterBase:=OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN) THEN
  204.         Raise('Failed to open muimaster.library')
  205.  
  206.  
  207. /* Create the new custom class with a call TO MUI_CreateCustomClass(). */
  208. /* Caution: This function returns not a struct IClass, but a           */
  209. /* struct MUI_CustomClass which contains a struct IClass to be         */
  210. /* used with NewObject() calls.                                        */
  211. /* Note well: MUI creates the dispatcher hook for you, you may         */
  212. /* *not* use its h_Data field! If you need custom data, use the        */
  213. /* cl_UserData OF the IClass structure!                                */
  214.  
  215.     IFN mcc:=MUI_CreateCustomClass(NIL,MUIC_Area,NIL,SIZEOF_mydata,&MyDispatcher) THEN
  216.         Raise('Could not create custom class.')
  217.  
  218.     app:=ApplicationObject,
  219.         MUIA_Application_Title,      'Class3',
  220.         MUIA_Application_Version,    '$VER: Class3 12.9 (21.11.95)',
  221.         MUIA_Application_Copyright,  '©1995, Stefan Stuntz',
  222.         MUIA_Application_Author,     'Stefan Stuntz',
  223.         MUIA_Application_Description,'Demonstrate the use OF custom classes.',
  224.         MUIA_Application_Base,       'CLASS3',
  225.         SubWindow, window:=WindowObject,
  226.             MUIA_Window_Title,'A rather complex custom class',
  227.             MUIA_Window_ID,   "CLS3",
  228.             WindowContents,VGroup,
  229.                 Child,TextObject,
  230.                     TextFrame,
  231.                     MUIA_Background,MUII_TextBack,
  232.                     MUIA_Text_Contents, '\ecPaint with mouse,\nscroll with cursor keys.',
  233.                 End,
  234.                 Child,myobj:=NewObject(mcc.Class,NIL,TextFrame,TAG_DONE),
  235.             End,
  236.         End,
  237.     End
  238.  
  239.     IF app=NIL THEN Raise('Failed to create Application')
  240.  
  241.     set(window,MUIA_Window_DefaultObject,myobj)
  242.  
  243.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,MUI_TRUE,
  244.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit)
  245.  
  246.  
  247. /*
  248. ** This is the ideal input loop for an object oriented MUI application.
  249. ** Everything is encapsulated in classes, no return ids need to be used,
  250. ** we just check if the program shall terminate.
  251. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  252. ** from Wait() (or 0). This makes the input loop significantly faster.
  253. */
  254.  
  255.     set(window,MUIA_Window_Open,MUI_TRUE)
  256.  
  257.     WHILEN DoMethodA(app,[MUIM_Application_NewInput,&sigs])=MUIV_Application_ReturnID_Quit
  258.         IF sigs THEN sigs:=Wait(sigs)
  259.     ENDWHILE
  260.  
  261.     set(window,MUIA_Window_Open,FALSE)
  262.  
  263. /*
  264. ** Shut down...
  265. */
  266.  
  267. EXCEPTDO
  268.     IF app THEN MUI_DisposeObject(app)                /* dispose all objects. */
  269.     IF mcc THEN MUI_DeleteCustomClass(mcc)            /* delete the custom class. */
  270.     IF MUIMasterBase THEN CloseLibrary(MUIMasterBase) /* close library */
  271.     IF exception THEN PrintF('\s\n',exception)
  272. ENDPROC
  273.